<?php
class ControllerAccountForgotten extends Controller {
	private $error = array();

	public function index() {
		if ($this->customer->isLogged()) {
			$this->redirect($this->url->link('account/account', '', 'SSL'));
		}

		$this->language->load('account/forgotten');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('account/customer');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->language->load('mail/forgotten');

            if($this->request->post['method']=='email'){
                $email=$this->request->post['s_value'];
                $success_message= $this->language->get('text_success_email');
            }

            if($this->request->post['method']=='phone'){
                $customer=$this->model_account_customer->getCustomerByTelephone($this->request->post['s_value']);
                $email=$customer['email'];
                $n = strpos($email,'@');
                if($n<3){
                    $email_hide = substr_replace($email,"****",$n,0);
                }elseif ($n<6){
                    $email_hide = substr_replace($email,"****",2,$n-2);
                }else {
                    $email_hide = substr_replace($email,"****",2,4);
                }
                $success_message= sprintf($this->language->get('text_success_phone'), $email_hide);
            }


            //update console password
            $customer=$this->model_account_customer->getCustomerByEmail($email);


            $string = md5($customer['email'].'+'.$customer['password']);
            $string = 'p='.base64_encode($customer['email']."|".$string);


			$subject = sprintf($this->language->get('text_subject'), $this->config->get('config_name'));

			$message  = sprintf($this->language->get('text_greeting'), $this->config->get('config_name')) . "<br/><br/>";

            $link=$this->url->link('account/resetpass', $string, 'SSL');
            $message .= "<a href='".$link."'>".$link."</a>";

			$mail = new Mail();
			$mail->protocol = $this->config->get('config_mail_protocol');
			$mail->parameter = $this->config->get('config_mail_parameter');
			$mail->hostname = $this->config->get('config_smtp_host');
			$mail->username = $this->config->get('config_smtp_username');
			$mail->password = $this->config->get('config_smtp_password');
			$mail->port = $this->config->get('config_smtp_port');
			$mail->timeout = $this->config->get('config_smtp_timeout');
			$mail->setTo($email);
			$mail->setFrom($this->config->get('config_email'));
			$mail->setSender($this->config->get('config_name'));
			$mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
			$mail->setHtml(html_entity_decode($message, ENT_QUOTES, 'UTF-8'));
			$mail->send();

			$this->session->data['success'] =$success_message;

			$this->redirect($this->url->link('account/login', '', 'SSL'));
		}

		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home'),        	
			'separator' => false
		);

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_account'),
			'href'      => $this->url->link('account/account', '', 'SSL'),     	
			'separator' => $this->language->get('text_separator')
		);

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_forgotten'),
			'href'      => $this->url->link('account/forgotten', '', 'SSL'),       	
			'separator' => $this->language->get('text_separator')
		);

		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_your_email'] = $this->language->get('text_your_email');
		$this->data['text_email'] = $this->language->get('text_email');

		$this->data['entry_email'] = $this->language->get('entry_email');

		$this->data['button_continue'] = $this->language->get('button_continue');
		$this->data['button_back'] = $this->language->get('button_back');

		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		$this->data['action'] = $this->url->link('account/forgotten', '', 'SSL');

		$this->data['back'] = $this->url->link('account/login', '', 'SSL');

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/forgotten.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/account/forgotten.tpl';
		} else {
			$this->template = 'default/template/account/forgotten.tpl';
		}

		$this->children = array(
			'common/column_left',
			'common/column_right',
			'common/content_top',
			'common/content_bottom',
			'common/footer',
			'common/header'	
		);

		$this->response->setOutput($this->render());		
	}

	protected function validate() {

        if($this->request->post['method']=='email'){
            if (!isset($this->request->post['s_value'])) {
                $this->error['warning'] = $this->language->get('error_email');
            } elseif (!$this->model_account_customer->getTotalCustomersByEmail($this->request->post['s_value'])) {
                $this->error['warning'] = $this->language->get('error_email');
            }
        }
        if($this->request->post['method']=='phone'){
            if (!isset($this->request->post['s_value'])) {
                $this->error['warning'] = $this->language->get('error_phone');
            } elseif (!$this->model_account_customer->getTotalCustomersByTelephone($this->request->post['s_value'])) {
                $this->error['warning'] = $this->language->get('error_phone');
            }
        }

		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}
}
?>